package com.education.welco.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.education.common.pojo.AjaxResult;
import com.education.welco.pojo.dto.LearningStatisticsDto;
import com.education.welco.pojo.vo.StudyAnalysisParamVo;
import com.education.welco.service.OrderPaperAnalysisService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * zcp
 * 试卷分析and 学情分析 and 学习统计
 * 2022/10/21
 */
@RestController
@RequestMapping("/paperAnalysis")
public class OrderPaperAnalysisController {

    public static final Logger LOGGER = LogManager.getLogger(OrderPaperAnalysisController.class);

    @Autowired
    private OrderPaperAnalysisService orderPaperAnalysisService;

    //平均分，总分，最高分，最低分，平均作答时长，及格率，各个分段人数
    //1 80-89分以上
    //2 60-80分段
    //3 90-99分段
    //4 60-69分段
    //5 100分
    @GetMapping("/summary")
    public AjaxResult summary(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.summary(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("summary error", e);
            return AjaxResult.error();
        }
    }


    //试题类型分布
    @GetMapping("/paperQuestionTypeDistribution")
    public AjaxResult paperQuestionType(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.paperQuestionType(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("paperQuestionType error", e);
            return AjaxResult.error();
        }
    }

    //试题分数分布
    @GetMapping("/paperScoreDistribution")
    public AjaxResult paperScoreDistribution(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.paperScoreDistribution(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("paperScoreDistribution error", e);
            return AjaxResult.error();
        }
    }

    //易错题top10
    @GetMapping("/fallibleQuestionTop")
    public AjaxResult fallibleQuestionTop(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.fallibleQuestionTop(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("fallibleQuestionTop error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 知识点分布不做
     */
    /*@GetMapping("/knowledgePointsDistribution")
    public AjaxResult knowledgePointsDistribution(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.knowledgePointsDistribution(paperId);
            return AjaxResult.success(map);
        }catch (Exception e){
            LOGGER.error("knowledgePointsDistribution error", e);
            return AjaxResult.error();
        }
    }*/

    /**
     * 每道题选择人数
     *
     * @param paperId
     * @return
     */
    @GetMapping("/itemAnalysis")
    public AjaxResult itemAnalysis(@RequestParam(value = "paperId", required = true) Integer paperId) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.itemAnalysis(paperId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("itemAnalysis error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 课程分析-课堂数据总览
     * 视频学习总学时(小时)、线上考核参与人次、平均学习进度、测试及格率
     *
     * @param courseId
     * @param classId
     * @return
     */
    @GetMapping("/dataScreening")
    public AjaxResult dataScreening(@RequestParam(value = "courseId") Integer courseId,
                                    @RequestParam(value = "classId") Integer classId
    ) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.dataScreening(courseId, classId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("dataScreening error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 学习进度
     *
     * @param courseId
     * @param classId
     * @return
     */
    @GetMapping("/rateOfLearning")
    public AjaxResult rateOfLearning(@RequestParam(value = "courseId", required = true) Integer courseId,
                                     @RequestParam(value = "classId", required = true) Integer classId
    ) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.rateOfLearning(courseId, classId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("rateOfLearning error", e);
            return AjaxResult.error();
        }
    }

    /**
     * 视频学习情况
     *
     * @param paramVo
     * @return
     */
    @PostMapping("/videoLearning")
    public AjaxResult videoLearning(@RequestBody StudyAnalysisParamVo paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.videoLearning(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("videoLearning error", e);
            return AjaxResult.error();
        }
    }


    /**
     * 考核完成情况
     *
     * @param paramVo
     * @return
     */
    @PostMapping("/assessmentCompletion")
    public AjaxResult assessmentCompletion(@RequestBody StudyAnalysisParamVo paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.assessmentCompletion(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("assessmentCompletion error", e);
            return AjaxResult.error();
        }
    }

    //内容榜单
    @PostMapping("/contentList")
    public AjaxResult contentList(@RequestBody StudyAnalysisParamVo paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.contentList(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("contentList error", e);
            return AjaxResult.error();
        }
    }

    //参与率最高的讨论主题TOP3
    @PostMapping("/talkTop")
    public AjaxResult talkTop(@RequestBody StudyAnalysisParamVo paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.talkTop(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("talkTop error", e);
            return AjaxResult.error();
        }
    }

    //app学习统计top
    @PostMapping("/learningStatistics")
    public AjaxResult learningStatistics(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.learningStatistics(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("learningStatistics error", e);
            return AjaxResult.error();
        }
    }

    //app错题本
    @PostMapping("/wrongTitleBook")
    public AjaxResult wrongTitleBook(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.wrongTitleBook(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("wrongTitleBook error", e);
            return AjaxResult.error();
        }
    }

    //错题排行榜
    @PostMapping("/wrongQuestionRanking")
    public AjaxResult wrongQuestionRanking(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.wrongQuestionRanking(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("wrongQuestionRanking error", e);
            return AjaxResult.error();
        }
    }

    //答对题排行榜
    @PostMapping("/correctQuestionsRanking")
    public AjaxResult correctQuestionsRanking(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.correctQuestionsRanking(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("correctQuestionsRanking error", e);
            return AjaxResult.error();
        }
    }

    //练习时长排行榜
    @PostMapping("/exerciseDurationRanking")
    public AjaxResult exerciseDurationRanking(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.exerciseDurationRanking(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("exerciseDurationRanking error", e);
            return AjaxResult.error();
        }
    }


    //正确率排行榜
    @PostMapping("/accuracyRanking")
    public AjaxResult accuracyRanking(LearningStatisticsDto paramVo) {
        try {
            Map<String, Object> map = orderPaperAnalysisService.accuracyRanking(paramVo);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("accuracyRanking error", e);
            return AjaxResult.error();
        }
    }

    /**
     * app 错题本 开始答题接口 答错的试题
     *
     * @param paperId 试卷id
     * @param userId  用户id
     * @return
     */
    @PostMapping(value = "/selectWrongQuestions")
    public AjaxResult selectWrongQuestions(@RequestParam("paperId") Integer paperId,
                                           @RequestParam("userId") Integer userId) {
        if (paperId == null || userId == null) {
            return new AjaxResult(201, "缺少相关参数", "");
        }
        try {
            Map<String, Object> map = orderPaperAnalysisService.selectWrongQuestions(paperId, userId);
            return AjaxResult.success(map);
        } catch (Exception e) {
            LOGGER.error("selectWrongQuestions error", e);
            return new AjaxResult(500, e.getMessage(), "");
        }
    }

    /**
     * 复习题提交
     *
     * @param id
     * @param xsAnswer
     * @param stAnswer
     * @return
     */
    @PostMapping(value = "/subWrongQuestions")
    public AjaxResult subWrongQuestions(String id, String xsAnswer, String stAnswer) {
        try {
            if (compare(xsAnswer, stAnswer)) {
                List<Integer> list = Arrays.stream(id.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
                if (CollectionUtil.isEmpty(list)) {
                    return new AjaxResult(201, "缺少相关参数", "");
                }
                orderPaperAnalysisService.subWrongQuestions(list);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            LOGGER.error("subWrongQuestions error", e);
            return new AjaxResult(500, e.getMessage(), "");
        }
    }

    private boolean compare(String answer1, String answer2) {
        char[] charArray1 = answer1.toCharArray();
        char[] charArray2 = answer2.toCharArray();
        Arrays.sort(charArray1);
        Arrays.sort(charArray2);
        String s1 = String.valueOf(charArray1);
        String s2 = String.valueOf(charArray2);
        return s1.equals(s2);
    }

}
